// 小程序开发api接口工具包，https://github.com/gooking/wxapi
const CONFIG = require('./config.js')
const API_BASE_URL = CONFIG.apiUrl
const util = require('./../utils/util.js')


const request = (url, method, data) => {
  let _url = API_BASE_URL + url
  util.showLoading('加载中...')
  return new Promise((resolve, reject) => {
    wx.request({
      url: _url,
      method: method,
      data: data,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success(request) {
        util.hideLoading()
        resolve(request.data)
      },
      fail(error) {
        util.hideLoading()
        reject(error)
      },
      complete(aaa) {
        // 加载完成
      }
    })
  })
}

/**
 * 小程序的promise没有finally方法，自己扩展下
 */
Promise.prototype.finally = function (callback) {
  var Promise = this.constructor;
  return this.then(
    function (value) {
      Promise.resolve(callback()).then(
        function () {
          return value;
        }
      );
    },
    function (reason) {
      Promise.resolve(callback()).then(
        function () {
          throw reason;
        }
      );
    }
  );
}

const request_unload = (url, method, data) => {
  let _url = API_BASE_URL + url
  return new Promise((resolve, reject) => {
    wx.request({
      url: _url,
      method: method,
      data: data,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success(request) {
        resolve(request.data)
      },
      fail(error) {
        reject(error)
      },
      complete(aaa) {
        // 加载完成
      }
    })
  })
}

module.exports = {
  request,  
  // 2024-2-2
  loadReqList: (data) => {
    return request('/req/loadReqListByStatus', 'get', data)
  },
  loadMyReqList: (data) => {
    return request('/req/loadMyReqList', 'get', data)
  },
  loadYourReqList: (data) => {
    return request('/req/loadYourReqList', 'get', data)
  },
  loadReqOne: (data) => {
    return request('/req/loadReqById', 'get', data)
  },
  saveReq: (data) => {
    return request('/req/saveReq', 'post', data)
  },
  completeReq: (data) => {
    return request('/req/completeReq', 'post', data)
  },
  giveupReq: (data) => {
    return request('/req/giveupReq', 'post', data)
  },
  resetReq: (data) => {
    return request('/req/resetReq', 'post', data)
  },
  deleteReq: (data) => {
    return request('/req/deleteReq', 'post', data)
  },
  loadTaskByReqId: (data) => {
    return request('/task/loadTaskByReqId', 'get', data)
  },
  loadTaskByTaskId:  (data) => {
    return request('/task/loadTaskByTaskId', 'get', data)
  },
  loadTaskByTaskOpenId:  (data) => {
    return request('/task/loadTaskByTaskOpenId', 'get', data)
  },
  loadTaskWordByTaskId: (data) => {
    return request_unload('/task/loadTaskWordByTaskId', 'get', data)
  },
  receiveReq: (data) => {
    return request('/task/receiveReq', 'post', data)
  },
  saveTaskWord: (data) => {
    return request('/task/saveTaskWord', 'post', data)
  },
  initPhone: (data) => {
    return request('/users/initPhone', 'post', data)
  },
  initialUser:  (data) => {
    return request('/users/initialUser', 'post', data)
  },
  updateUserInfo: (data) => {
    return request('/users/updateUserInfo', 'post', data)
  },
  // 推荐相关
  listInviteUser: (data) => {
    return request_unload('/users/listInviteUser', 'get', data)
  },
  increaseScore: (data) => {
    return request('/users/increaseScore', 'post', data)
  },
  decrementScore: (data) => {
    return request('/users/decrementScore', 'post', data)
  },
  loadCompleteUserInfo: (data) => {
    return request('/users/loadCompleteUserInfo', 'get', data)
  },
  sendVCode: (data) => {
    return request('/users/sendVCode', 'post', data)
  },
  // 商业
  saveNetwork: (data) => {
    return request('/network/saveNetwork', 'post', data)
  },
  delNetwork: (data) => {
    return request('/network/delNetwork', 'post', data)
  },
  loadNetworkList: (data) => {
    return request('/network/loadNetworkList', 'get', data)
  },
  // 加载用户
  loadUser: (data) => {
    return request('/users/loadUser', 'get', data)
  },
  updateCustom: (data) => {
    return request('/users/updateCustom', 'post', data)
  },
  countWord: (data) => {  // 获取未读数量
    return request('/req/countWord', 'get', data)
  },
  resetWordCount: (data) => { // 清楚任务未读数量
    return request('/task/resetWordCount', 'post', data)
  },
  // 2024-8-2
  loadGift: (data) => {  // 获取礼物
    return request('/gift/loadGift', 'get', data)
  },

  // 2024-8-13
  updateAvatar: (data) => {
    return request('/users/updateAvatar', 'post', data)
  }
}
